<template>
  <div id="app">
    <el-container v-if="isAuthenticated">
      <el-header style="padding: 0; height: auto;">
        <el-menu
          mode="horizontal"
          :default-active="currentRoute"
          :router="true"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
        >
          <el-menu-item index="/">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/students">
            <el-icon><User /></el-icon>
            <span>学生管理</span>
          </el-menu-item>
          <el-menu-item index="/rooms">
            <el-icon><OfficeBuilding /></el-icon>
            <span>机房管理</span>
          </el-menu-item>
          <el-menu-item index="/machines">
            <el-icon><Monitor /></el-icon>
            <span>机器管理</span>
          </el-menu-item>
          <el-menu-item index="/sessions">
            <el-icon><Timer /></el-icon>
            <span>上机管理</span>
          </el-menu-item>
          <el-menu-item style="margin-left: auto;" @click="handleLogout">
            <el-icon><SwitchButton /></el-icon>
            <span>退出登录</span>
          </el-menu-item>
        </el-menu>
      </el-header>
      <el-main style="padding: 20px; min-height: calc(100vh - 60px);">
        <router-view />
      </el-main>
    </el-container>
    
    <!-- 确保未认证时的路由视图正确渲染 -->
    <div v-else>
      <router-view />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  House,
  User,
  OfficeBuilding,
  Monitor,
  Timer,
  SwitchButton
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const store = useStore()

// 计算属性
const isAuthenticated = computed(() => store.state.isAuthenticated)
const currentRoute = computed(() => route.path)

// 退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm(
      '确定要退出登录吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    store.dispatch('logout')
    ElMessage.success('退出登录成功')
    router.push('/login')
  } catch (error) {
    // 用户取消操作
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#app {
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB',
    'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  background-color: #f5f7fa;
}

.el-header {
  padding: 0;
}

.el-menu {
  border-bottom: none;
}

.el-menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
}
</style>